<?php
namespace app\admin\controller\goods;

use app\common\model\Goods as Model;
use app\common\model\Supplier as Supplier;

use app\admin\classes\BasicManager;
use core\Api;
use core\V;
use core\Res;

/**
 * 产品
 */


class Goods extends BasicManager {

  static $model=Model::class;

  // static $hash="/admin/goods/goods";

  static $var="id,code,name,unit,supplier_id,
  des,remark";

  static $title=[
    "index"=>[
      "title"=>"产品管理",
      "breadcrumb"=>["产品","产品管理","列表"],
      "subtitle"=>"管理相册中的图片",
    ],
    "add"=>[
      "title"=>"添加产品",
      "breadcrumb"=>["产品","产品管理","添加"],
      "subtitle"=>"添加产品资源",
    ],
    "edit"=>[
      "title"=>"编辑产品",
      "breadcrumb"=>["产品","产品管理","编辑"],
      "subtitle"=>"编辑产品资源",
    ],

  ];


  public function getlist_filter(&$item){
    return [

    ];
  }
  public function add_filter(){
    return [
      "date"=>date("Y-m-d H:i:s"),
      "unit"=>"个",
    ];
  }
  public function save_filter(&$data){
    empty($data["code"]) and Api::error('请输入编码');
    empty($data["name"]) and Api::error('请输入名称');
    empty($data["unit"]) and Api::error('请输入单位');
    empty($data["supplier_id"]) and Api::error('请选择供应商');

    $this->db_has($data,"code") and Api::error('编码已存在');
    $this->db_has($data,"name") and Api::error('名称已存在');

    return $data;
  }
  public function getAllSupplier(){

    $all=Supplier::where("status",1)
                 ->column('name','id');
    $result=[];

    foreach ($all as $key => $value) {
      $result[]=[
        "value"=>$key,
        "label"=>$value,
      ];
    }

    Api::ok($result);

  }

   public function edit_body(){
     $this->VD([
       "___vue___"=>'
       data(){
         return {
           supplierList:[]
         }
       },
       methods: {
          queryUnit(queryString, cb) {
            console.log("a",queryString);
            cb([
              {value:"个"}
            ]);
          },
          onAddFinish(res){
            this.form.code="";
            this.form.name="";
            this.form.des="";
            this.form.remark="";
          }
        },
       mounted(){
         var that=this;
         this.$nextTick(function () {
           APP.ajax(that.apiBase+"/getAllSupplier",{},function(msg){
             if (msg.result){
               that.supplierList=msg.data;
             }else{
               APP.alert(msg.data);
             }
           });
         })
       }

       '
     ]);
     return '
     <el-form-item label="编码">
       <el-input v-model="form.code"></el-input>
     </el-form-item>

     <el-form-item label="名称">
       <el-input v-model="form.name"></el-input>
     </el-form-item>

     <el-form-item label="单位">
       <el-autocomplete
        :fetch-suggestions="queryUnit"
        v-model="form.unit">
       </el-autocomplete>
     </el-form-item>

     <el-form-item label="供应商">
      <el-select v-model="form.supplier_id" filterable placeholder="请选择">
        <el-option
          v-for="item in supplierList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
     </el-form-item>

     <el-form-item label="描述">
       <el-input type="textarea" v-model="form.des"></el-input>
     </el-form-item>


     <el-form-item label="备注">
       <el-input v-model="form.remark"></el-input>
     </el-form-item>


     ';
   }
   public function index_body(){
     // $this->VD([
     //   "___btn___"=>'
     //      <div style="padding-right: 10px;">
     //        <el-button
     //          @click="printQRcode(scope.row.id)"
     //          type="text"
     //          size="small"
     //        >二维码</el-button>
     //      </div>
     //   ',
     //   "___vue___"=>'
     //
     //       methods:{
     //         printQRcode(id){
     //           APP.ajax("/admin/goods/recommend/set",{id:id},function(msg){
     //             if (msg.result){
     //               APP.alert(msg.data,true);
     //             }else{
     //               APP.alert(msg.data);
     //             }
     //           });
     //         }
     //       }
     //
     //   '
     // ]);
     return '
     <el-table-column
       prop="code"
       label="编码"
       width="120">
     </el-table-column>

     <el-table-column
       prop="name"
       label="名称"
       width="180">
     </el-table-column>

     <el-table-column
       prop="unit"
       label="单位"
       width="80">
     </el-table-column>

     <el-table-column
       prop="des"
       label="描述">
     </el-table-column>

     <el-table-column
       prop="remark"
       label="备注">
     </el-table-column>



     ';
   }
   public function search_body(){
     return '
     <el-form-item label="">
       <el-input
         v-model="form.name"
         @keyup.enter.native="Search"
         placeholder="名称">
       </el-input>
     </el-form-item>
     <el-form-item label="">
       <el-input
         v-model="form.des"
         @keyup.enter.native="Search"
         placeholder="描述">
       </el-input>
     </el-form-item>

     <el-form-item label="">
       <el-input
         v-model="form.remark"
         @keyup.enter.native="Search"
         placeholder="备注">
       </el-input>
     </el-form-item>



     ';
   }



 }
